<template>
    <div class="search">
        <Card>
            <div v-if="showDrawer">
                <addEdit :dataValue="form" :type="showType" :is-zf="isZf" @on-submit="getDataList" @goBack="goBack" />
            </div>
            <div v-else>
                <!--      提示-->
                <div>
                    <Alert show-icon>
                        毕业生毕业去向落实后，应及时登记毕业去向，上报学校审核
                        <template #desc>
                            <Row>1，完成网上签约、签劳动合同、使用学校发的协议书签约或其他录用形式就业后，请及时登记就业信息，上传就业证明材料；</Row>
                            <Row>2，升学、出国出境、自主创业、应征入伍、自由职业等其它去向的学生，请及时登记去向信息，上传毕业去向证明材料。</Row>
                        </template>
                    </Alert>
                    <!--          20230918-->
                    <Alert type="warning" show-icon v-if="bysData[0].result == '3'">您的毕业去向申请被驳回，请修改</Alert>
                    <Alert type="error" show-icon v-if="bysData[0].status == '9'">您的毕业去向申请不通过，请重新填报</Alert>
                    <!--          20230918-->
                    <Alert type="success" show-icon v-if="bysData[0].result == '2'">您的毕业去向申请已通过</Alert>
                </div>
                <!--      毕业生信息-->
                <div class="InformationCon">
                    <Table :loading="loading" border :columns="byscolumns" :data="bysData" ref="bysTable"></Table>
                </div>
            </div>

            <div class="changeRecord">
                <Col span="24" class="demo-tabs-style2">
                    <Tabs type="card">
                        <TabPane label="审核记录">
                            <historicDetail ref="historic" v-if="showHistory" :procInstId="procInstId" />
                        </TabPane>
                        <TabPane label=" 变更记录">
                            <Table :height="height1" :loading="bgloading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort"></Table>
                            <Row type="flex" justify="end" class="page">
                                <Page
                                    :current="searchForm.pageNumber"
                                    :total="total"
                                    :page-size="searchForm.pageSize"
                                    @on-change="changePage"
                                    @on-page-size-change="changePageSize"
                                    :page-size-opts="[10, 20, 50, 100, 200]"
                                    size="small"
                                    show-total
                                    show-elevator
                                    show-sizer
                                ></Page>
                            </Row>
                        </TabPane>
                    </Tabs>
                </Col>
            </div>
        </Card>
        <Modal title="确认撤回" v-model="modalCancelVisible" :mask-closable="false" :width="500">
            <Form ref="delForm" v-model="cancelForm" :label-width="70">
                <FormItem label="撤回原因" prop="reason">
                    <Input type="textarea" v-model="cancelForm.reason" :rows="4" />
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="modalCancelVisible = false">取消</Button>
                <Button type="primary" :loading="submitLoading" @click="handelSubmitCancel">提交</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
// 根据你的实际请求api.js位置路径修改
import historicDetail from '@/views/activiti/historic-detail/historicDetail_bys.vue';
import { getJyqxRecordList, deleteJyqxRecord } from '@/api/bys/jyqx/record.js';
import { zfJyqxInfo, jyqxCancel, bysApplyBusiness } from '@/api/bys/jyqx/jyqx.js';
// import {
//   getProcessDataList,
//   getFirstNode,
//   getBusinessDataList,
//   applyBusiness,
//   deleteBusiness,
//   initActCategory,
//   loadActCategory,
// } from "@/api/activiti";

// 根据你的实际添加编辑组件位置路径修改
import addEdit from './jyqxInfoTb.vue';
import { shortcuts } from '@/libs/shortcuts';

export default {
    name: 'jyqxRecord',
    components: {
        addEdit,
        historicDetail
    },
    data() {
        return {
            actStatus: '',
            byscolumns: [
                {
                    title: '学校',
                    key: 'schoolName',
                    minWidth: 180,
                    sortable: false
                },
                {
                    title: '姓名',
                    key: 'name',
                    minWidth: 100,
                    sortable: false
                },
                {
                    title: '学号',
                    key: 'xh',
                    minWidth: 100,
                    sortable: false
                },
                {
                    title: '考生号',
                    key: 'ksh',
                    minWidth: 120,
                    sortable: false
                },
                {
                    title: '当前审批环节',
                    key: 'currTaskName',
                    minWidth: 120,
                    tooltip: true
                },
                {
                    title: '状态',
                    key: 'status',
                    align: 'center',
                    width: 120,
                    sortable: true,
                    render: (h, params) => {
                        let text = '待填报',
                            color = 'default';
                        if (params.row.status == 0 || params.row.status == 4) {
                            return h('div', [
                                h(
                                    'Tag',
                                    {
                                        props: {
                                            color: color
                                        }
                                    },
                                    text
                                )
                            ]);
                        }
                        if (params.row.status == 1 || params.row.status == 2 || params.row.status == 7 || params.row.status == 8) {
                            text = '草稿';
                            color = 'default';
                        } else if (params.row.status == 3) {
                            text = '审核中';
                            color = 'blue';
                        } else if (params.row.status == 5) {
                            text = '已撤回';
                            color = 'magenta';
                        } else if (params.row.status == -1) {
                            text = '已作废';
                            color = 'red';
                        }
                        if (params.row.result == 2) {
                            text = '已通过';
                            color = 'green';
                        } else if (params.row.result == 3) {
                            text = '已驳回';
                            color = 'orange';
                        }
                        return h('div', [
                            h(
                                'Tag',
                                {
                                    props: {
                                        color: color
                                    }
                                },
                                text
                            )
                        ]);
                    }
                },
                // {
                //   title: "结果",
                //   key: "result",
                //   align: "center",
                //   width: 120,
                //   sortable: true,
                //   render: (h, params) => {
                //     let text = "未填报",
                //         color = "default";
                //     if (params.row.result == 0) {
                //       text = "未提交";
                //       color = "default";
                //     } else if (params.row.result == 1) {
                //       text = "处理中";
                //       color = "orange";
                //     } else if (params.row.result == 2) {
                //       text = "已通过";
                //       color = "green";
                //     } else if (params.row.result == 3) {
                //       text = "已驳回";
                //       color = "red";
                //     }
                //     return h("div", [
                //       h(
                //           "Tag",
                //           {
                //             props: {
                //               color: color,
                //             },
                //           },
                //           text
                //       ),
                //     ]);
                //   },
                // },
                {
                    title: '提交申请时间',
                    key: 'applyTime',
                    width: 180,
                    sortable: true
                },
                {
                    title: '操作',
                    key: 'action',
                    align: 'center',
                    width: 180,
                    fixed: 'right',
                    render: (h, params) => {
                        // 20230918
                        if (params.row.status == 0 || params.row.status == 4 || params.row.status == 9) {
                            // 20230918
                            return [
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    '填报'
                                )
                            ];
                        }
                        if (params.row.status == -1) {
                            return [
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.zfdetail(params.row);
                                            }
                                        }
                                    },
                                    '详情'
                                )
                            ];
                        }

                        let re = '';
                        if (params.row.status == 1 || params.row.status == 2 || params.row.status == 7 || params.row.status == 8) {
                            re = [
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.apply(params.row);
                                            }
                                        }
                                    },
                                    '提交申请'
                                ),
                                h('Divider', {
                                    props: {
                                        type: 'vertical'
                                    }
                                }),
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    '填报'
                                )
                            ];
                        } else if (params.row.status == 3) {
                            re = [
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.cancel(params.row);
                                            }
                                        }
                                    },
                                    '撤回'
                                ),
                                // h("Divider", {
                                //   props: {
                                //     type: "vertical",
                                //   },
                                // }),
                                // h(
                                //     "a",
                                //     {
                                //       on: {
                                //         click: () => {
                                //           this.history(params.row);
                                //         },
                                //       },
                                //     },
                                //     "查看进度"
                                // ),
                                h('Divider', {
                                    props: {
                                        type: 'vertical'
                                    }
                                }),
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.detail(params.row);
                                            }
                                        }
                                    },
                                    '详情'
                                )
                            ];
                        } else if (params.row.status == 5) {
                            re = [
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.apply(params.row);
                                            }
                                        }
                                    },
                                    '重新申请'
                                ),
                                h('Divider', {
                                    props: {
                                        type: 'vertical'
                                    }
                                }),
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    '填报'
                                )
                                // h("Divider", {
                                //   props: {
                                //     type: "vertical",
                                //   },
                                // }),
                                // h(
                                //     "a",
                                //     {
                                //       on: {
                                //         click: () => {
                                //           this.history(params.row);
                                //         },
                                //       },
                                //     },
                                //     "审批历史"
                                // ),
                            ];
                        } else if (params.row.status == 6) {
                            re = [
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.zf(params.row);
                                            }
                                        }
                                    },
                                    '作废'
                                ),
                                h('Divider', {
                                    props: {
                                        type: 'vertical'
                                    }
                                }),
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.detail(params.row);
                                            }
                                        }
                                    },
                                    '详情'
                                )
                                // h("Divider", {
                                //   props: {
                                //     type: "vertical",
                                //   },
                                // }),
                                // h(
                                //     "a",
                                //     {
                                //       on: {
                                //         click: () => {
                                //           this.history(params.row);
                                //         },
                                //       },
                                //     },
                                //     "审批历史"
                                // ),
                            ];
                        }
                        if ((params.row.actStatus == 2 && params.row.result == 3) || params.row.actStatus == 3) {
                            re = [
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.apply(params.row);
                                            }
                                        }
                                    },
                                    '重新申请'
                                ),
                                h('Divider', {
                                    props: {
                                        type: 'vertical'
                                    }
                                }),
                                h(
                                    'a',
                                    {
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    '填报'
                                )
                                // h("Divider", {
                                //   props: {
                                //     type: "vertical",
                                //   },
                                // }),
                                // h(
                                //     "a",
                                //     {
                                //       on: {
                                //         click: () => {
                                //           this.history(params.row);
                                //         },
                                //       },
                                //     },
                                //     "审批历史"
                                // ),
                            ];
                        }
                        return h('div', re);
                    }
                }
                // {
                //   title: "操作",
                //   key: "action",
                //   align: "center",
                //   width: 150,
                //   render: (h, params) => {
                //     return h("div", [
                //       h(
                //           "Button",
                //           {
                //             props: {
                //               type: 'primary',
                //               // size:'small',
                //             },
                //             on: {
                //               click: () => {
                //                 this.edit(params.row);
                //               }
                //             }
                //           },
                //           "填报"
                //       )
                //     ]);
                //   }
                // }
            ],
            bysData: [],
            openTip: true, // 显示提示
            showType: '0', // 添加或编辑标识
            showDrawer: false, // 显示添加编辑抽屉
            loading: true, // 表单加载状态
            bgloading: true, // 表单加载状态
            height1: 0,
            searchForm: {
                // 搜索框初始化对象
                pageNumber: 1, // 当前页数
                pageSize: 50, // 页面大小
                sort: 'createTime', // 默认排序字段
                order: 'desc' // 默认排序方式
            },
            selectList: [], // 多选数据
            form: {},
            columns: [
                {
                    type: 'index',
                    title: '序号',
                    align: 'center'
                },
                {
                    title: '用户',
                    key: 'opr',
                    sortable: false
                },
                {
                    title: '操作',
                    key: 'op',
                    sortable: false,
                    render: (h, params) => {
                        let text = '待填报',
                            color = 'default';
                        if (!params.row.op) {
                            return h('div', [
                                h(
                                    'Tag',
                                    {
                                        props: {
                                            color: color
                                        }
                                    },
                                    text
                                )
                            ]);
                        }
                        if (params.row.op == 0) {
                            text = '填报';
                            color = 'default';
                        } else if (params.row.op == 1) {
                            text = '申请';
                            color = 'orange';
                        } else if (params.row.op == 2) {
                            text = '撤回';
                            color = 'blue';
                        } else if (params.row.op == 3) {
                            text = '作废';
                            color = 'red';
                        }
                        return h('div', [
                            h(
                                'Tag',
                                {
                                    props: {
                                        color: color
                                    }
                                },
                                text
                            )
                        ]);
                    }
                },
                {
                    title: '操作时间',
                    key: 'createTime',
                    sortable: false
                },
                {
                    title: '备注',
                    key: 'remark',
                    sortable: false
                }
            ],
            data: [], // 表单数据
            pageNumber: 1, // 当前页数
            pageSize: 50, // 页面大小
            total: 0, // 表单数据总数

            modalCancelVisible: false,
            cancelForm: {
                reason: ''
            },
            submitLoading: false, // 添加或编辑提交状态
            userLoading: false,
            isGateway: false,
            showAssign: false,
            isCustom: false,
            showHistory: false,
            procInstId: '',
            isZf: '',
            zfList: [],
            qxList: []
        };
    },
    watch: {
        zfList(val, oldVal) {
            this.bysData = [];
            this.bysData.push(...this.qxList);
            this.bysData.push(...val);
        },
        qxList(val, oldVal) {
            this.bysData = [];
            this.bysData.push(...val);
            this.bysData.push(...this.zfList);
        }
    },
    methods: {
        tableheig() {
            var bbb = document.getElementsByClassName('calc-px-height')[0].scrollHeight;
            var aaa = document.getElementsByClassName('calc-px-height')[0].children[0].children[0].children[1];
            var ccc = document.getElementsByClassName('calc-px-height')[0].children[0].children[0].children[0].scrollHeight;
            var ddd = bbb - ccc;
            console.log(bbb, '00000000000000');
            aaa.style.height = `${ddd}px`;

            if (this.searchForm.pageSize === 10) {
                this.height1 = 0;
            } else {
                this.height1 = document.getElementsByClassName('calc-px-height')[0].scrollHeight;
                if (this.total < this.searchForm.pageNumber * this.searchForm.pageSize) {
                    var r = (this.searchForm.pageNumber - 1) * this.searchForm.pageSize;
                    var a = this.total - r;
                    var s = a * 43 + 65;
                    if (s < this.height1) {
                        this.height1 = 0;
                    }
                }
            }
            if (this.total < 10) {
                this.height1 = 0;
            }
        },
        init() {
            this.getBysData();
            this.getDataList();
            // this.getZfRecord();
        },
        async getZfRecord() {
            this.zfList = [];
            this.loading = true;
            let res = await this.postRequest('/bysJyqxRecord/getZfRecord');
            this.loading = false;
            if (res.success) {
                res.result.forEach(el => {
                    this.zfList.push({
                        schoolName: el.gxmc,
                        name: el.xm,
                        xh: el.xh,
                        ksh: el.ksh,
                        currTaskName: '-',
                        status: '-1',
                        applyTime: el.applyTime,
                        id: el.id,
                        userid: el.userid
                    });
                });
            }
        },
        zf(v) {
            this.$Modal.confirm({
                title: '确认作废',
                // 记得确认修改此处
                content: '您确认要作废此条毕业生去向申请 ?',
                loading: true,
                onOk: () => {
                    zfJyqxInfo({ id: v.userid }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.clearSelectAll();
                            this.$Message.success('操作成功');
                            // this.init();
                            this.getBysData();
                        }
                    });
                }
            });
        },
        detail(v) {
            this.showType = '2';
            this.showDrawer = true;
            this.form = v;
            // 20230921
            this.isZf = '';
            // 20230921
            // let query = { type: 2, id: v.tableId, backRoute: this.$route.name };
            // this.$router.push({
            //   name: v.routeName,
            //   query: query,
            // });
        },
        zfdetail(v) {
            // 转换null为""
            for (let attr in v) {
                if (v[attr] == null) {
                    v[attr] = '';
                }
            }
            let str = JSON.stringify(v);
            let data = JSON.parse(str);

            this.showType = '2';
            this.showDrawer = true;
            this.form = data;
            this.isZf = '1';
        },
        // history(v) {
        //   if (!v.procInstId) {
        //     this.$Message.error("流程实例ID不存在");
        //     return;
        //   }
        //   this.procInstId = v.procInstId;
        //   this.showHistory = true;
        // },
        apply(v) {
            this.$Modal.confirm({
                title: '确认提交',
                // 记得确认修改此处
                content: '您确认要提交毕业生去向申请 ?',
                loading: true,
                onOk: () => {
                    bysApplyBusiness({ id: v.eId, routeName: 'jyqxInfoTb' }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.clearSelectAll();
                            this.$Message.success('操作成功');
                            // this.init();
                            this.getBysData();
                        }
                    });
                }
            });
        },
        cancel(v) {
            this.cancelForm.id = v.eId;
            this.cancelForm.procInstId = v.procInstId;
            this.modalCancelVisible = true;
        },
        handelSubmitCancel() {
            this.submitLoading = true;
            jyqxCancel(this.cancelForm).then(res => {
                this.submitLoading = false;
                if (res.success) {
                    this.$Message.success('操作成功');
                    this.getBysData();
                    // this.$refs.historic.init();
                    this.modalCancelVisible = false;
                }
            });
        },
        async getBysData() {
            this.qxList = [];
            this.loading = true;
            let res = await this.postRequest('/bysPersonInfo/getBysInfo');
            this.loading = false;
            if (res.success) {
                this.actStatus = res.result.actStatus;
                this.qxList.push(res.result);
                this.procInstId = res.result.procInstId;
                this.getZfRecord();
                this.showHistory = true;
                console.log(this.procInstId);
                if (this.procInstId) {
                    this.$refs.historic.procInstId = this.procInstId;
                    this.$refs.historic.init();
                    this.getDataList();
                }
            }
        },
        // 从新增/编辑页面返回时间
        goBack() {
            // window.location.reload();

            this.getBysData();
            // this.getDataList();
            this.showDrawer = !this.showDrawer;
        },
        changePage(v) {
            this.searchForm.pageNumber = v;
            this.getDataList();
            this.clearSelectAll();
        },
        changePageSize(v) {
            this.searchForm.pageSize = v;
            this.getDataList();
        },
        handleReset() {
            this.$refs.searchForm.resetFields();
            this.searchForm.pageNumber = 1;
            this.searchForm.pageSize = 50;
            // 重新加载数据
            this.getDataList();
        },
        changeSort(e) {
            this.searchForm.sort = e.key;
            this.searchForm.order = e.order;
            if (e.order === 'normal') {
                this.searchForm.order = '';
            }
            this.getDataList();
        },
        clearSelectAll() {
            this.$refs.table.selectAll(false);
        },
        changeSelect(e) {
            this.selectList = e;
        },
        getDataList() {
            this.bgloading = true;
            getJyqxRecordList(this.searchForm).then(res => {
                this.bgloading = false;
                if (res.success) {
                    this.data = res.result.records;
                    this.total = res.result.total;
                    this.tableheig();
                    // if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
                    //   this.searchForm.pageNumber -= 1;
                    //   this.getDataList();
                    // }
                }
            });
        },
        add() {
            this.showType = '2';
            this.showDrawer = true;
        },
        edit(v) {
            // 转换null为""
            for (let attr in v) {
                if (v[attr] == null) {
                    v[attr] = '';
                }
            }
            let str = JSON.stringify(v);
            let data = JSON.parse(str);

            this.showType = '1';
            this.showDrawer = true;
            this.form = data;
            this.isZf = '';
            console.log(this.form, '------form');
        },
        remove(v) {
            this.$Modal.confirm({
                title: '确认删除',
                // 记得确认修改此处
                content: '您确认要删除该条数据?',
                loading: true,
                onOk: () => {
                    // 删除
                    deleteJyqxRecord({ ids: v.id }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success('操作成功');
                            this.clearSelectAll();
                            this.getDataList();
                        }
                    });
                }
            });
        },
        delAll() {
            if (this.selectList.length <= 0) {
                this.$Message.warning('您还未选择要删除的数据');
                return;
            }
            this.$Modal.confirm({
                title: '确认删除',
                content: '您确认要删除所选的 ' + this.selectList.length + ' 条数据?',
                loading: true,
                onOk: () => {
                    let ids = '';
                    this.selectList.forEach(function (e) {
                        ids += e.id + ',';
                    });
                    ids = ids.substring(0, ids.length - 1);
                    // 批量删除
                    deleteJyqxRecord({ ids: ids }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success('操作成功');
                            this.clearSelectAll();
                            this.getDataList();
                        }
                    });
                }
            });
        }
    },
    mounted() {
        this.init();
    }
};
</script>
<style lang="less">
// 建议引入通用样式 具体路径自行修改 可删除下面样式代码
// @import "../../../styles/table-common.less";

.search > .ivu-card {
    height: auto;
}

/deep/ .ivu-table-header {
    overflow-y: scroll;
}
/deep/ .ivu-table {
    .ivu-table-body {
        overflow-y: scroll;
        // height: 88.5% !important;
    }
}
.search {
    .operation {
        margin-bottom: 2vh;
    }

    .select-count {
        font-weight: 600;
        color: #40a9ff;
    }

    .select-clear {
        margin-left: 10px;
    }

    .page {
        margin-top: 2vh;
    }

    .drop-down {
        margin-left: 5px;
    }

    .InformationCon {
        margin: 20px 0;
    }

    .changeRecord {
        overflow: hidden;

        .ivu-tabs-nav-wrap {
            background: rgb(244, 244, 244);
        }

        .demo-tabs-style2 > .ivu-tabs.ivu-tabs-card > .ivu-tabs-bar .ivu-tabs-tab {
            border-radius: 0;
            background: #fff;
        }

        .demo-tabs-style2 > .ivu-tabs.ivu-tabs-card > .ivu-tabs-bar .ivu-tabs-tab-active {
            border-top: 1px solid #3399ff;
        }

        .demo-tabs-style2 > .ivu-tabs.ivu-tabs-card > .ivu-tabs-bar .ivu-tabs-tab-active:before {
            content: '';
            display: block;
            width: 100%;
            height: 1px;
            background: #3399ff;
            position: absolute;
            top: 0;
            left: 0;
        }
    }
}
</style>
